package member.base.action;

import m.common.action.ActionRequest;
import m.common.action.Param;
import m.common.action.RequestMethod;
import m.common.model.util.ModelCheckUtil;
import m.common.model.util.ModelQueryCheck;
import m.common.model.util.ModelUpdateUtil;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import member.base.model.BusinessInfo;
import member.base.model.StatusModel;
import member.base.model.StoreInfo;
import member.base.model.UserInfo;
import member.base.util.PowerUtil;

@ActionRequest(name = "action/base/businessInfo")
public class BusinessInfoAction extends BaseAction {

    @RequestMethod(description = "商家注册",transaction = true)
    public JSONMessage apply(@Param("business") BusinessInfo business, @Param("store") StoreInfo store, @Param("user") UserInfo user) throws Exception {
        ModelQueryCheck.init(business)
            .checkNotNull("name","商家名称不能为空")
            .checkNotNull("description","商家描述不能为空")
            .doCheck(false,true);
        business.setOid(GenerateID.generatePrimaryKey());
        business.setUser(user);
        business.setMemberDayType("N");
        business.setPointRate(100);
        business.setDiscount(0);
        ModelQueryCheck.init(store)
            .checkNotNull("name","首店名称不能为空")
            .doCheck(false,true);
        store.setOid(GenerateID.generatePrimaryKey());
        store.setBusiness(business);
        store.setUser(user);
        ModelQueryCheck.init(user)
            .checkNotNull("realName","姓名不能为空")
            .checkNotNull("phoneNum","手机号不能为空")
            .checkNotNull("password","密码不能为空")
            .doCheck(false,true);
        ModelCheckUtil.checkUniqueCombine(user,new String[]{"phoneNum"},"手机号已注册");
        user.setOid(GenerateID.generatePrimaryKey());
        user.setBusiness(business);
        user.setStore(store);
        user.setPowers(new String[]{UserInfo.PowerManage,UserInfo.PowerStore});
        user.setPassword(StringUtil.to32MD5(user.getPassword()));
        user.setStatus(StatusModel.StatusEnabled);
        ModelUpdateUtil.insertModel(business);
        ModelUpdateUtil.insertModel(store);
        ModelUpdateUtil.insertModel(user);
        return JSONMessage.success("注册成功");
    }
    @RequestMethod(description = "获取当前登录人商家信息")
    public JSONMessage get() throws Exception {
        return JSONMessage.success("获取成功")
            .push("model", PowerUtil.getBusiness());
    }
    @RequestMethod(description = "保存商家信息")
    public JSONMessage save(@Param("model") BusinessInfo model) throws Exception {
        PowerUtil.verifyPower(UserInfo.PowerManage);
        PowerUtil.verifyBusiness(model);
        ModelQueryCheck.init(model)
            .checkNotNull("name","商家名称不能为空")
            .checkNotNull("description","商家描述不能为空")
            .checkNotNull("pointRate","积分倍数不能为空")
            .checkNumberRange("pointRate",100.0,500.0,"积分倍数范围错误（100~500）")
            .checkNotNull("discount","优惠比例不能为空")
            .checkNumberRange("discount",0.0,100.0,"优惠比例范围错误（0~100）")
            .doCheck(false,true);
        ModelUpdateUtil.updateModel(model,"name","description","memberDayType","memberDayNo","memberWeekNo","pointRate","discount");
        PowerUtil.clearBusiness(model.getOid());
        return JSONMessage.success("保存成功");
    }
}
